package lrl.nsfw.user.dao.Impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import lrl.core.dao.impl.BaseDaoImpl;
import lrl.nsfw.user.dao.UserDao;
import lrl.nsfw.user.entity.User;
import lrl.nsfw.user.entity.UserRole;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public List<User> findUserByAccountAndId(String account, String id) {
		
		String hqlString = "FROM User WHERE account = ?";
		if (StringUtils.isNotBlank(id)) {
			hqlString += " AND id = ?";
		}
		Query query = getSession().createQuery(hqlString);
		query.setParameter(0, account);
		if (StringUtils.isNotBlank(id)) {
			query.setParameter(1, id);
		}
		return query.list();
	}

	@Override
	public void saveUserRole(UserRole userRole) {
		
		getHibernateTemplate().save(userRole);
		
	}

	@Override
	public void deleteUserRoleByUserId(String id) {
		Query query = getSession().createQuery("DELETE FROM UserRole WHERE id.userId=?");
		query.setParameter(0, id);
		query.executeUpdate();
		
	}

	@Override
	public List<UserRole> getUserRolesByUserId(String id) {
		Query query = getSession().createQuery("FROM UserRole WHERE id.userId=?");
		query.setParameter(0, id);
		return query.list();
	}

	@Override
	public List<User> findObjectsByAccountAndPass(String account,
			String password) {
		Query query = getSession().createQuery("FROM User WHERE account=? AND password=?");
		query.setParameter(0, account);
		query.setParameter(1, password);
		return query.list();
	}

	

}
